<?php
declare (strict_types = 1);

namespace app\api\service;

class ApiResponse
{
    /**
     * 请求成功响应
     * @MethodAuthor: 聂哥
     * @Date: 2023-03-03 12:22:35
     */
    public static function success($data)
    {
        $code = empty($data) ? StatusCode::NOTFOUND : StatusCode::SUCCESS;
        return json([
            'code' => $code,
            'msg'  => StatusCode::getMessage($code),
            'data' => $data
        ]);
    }

    /**
     * 创建响应输出
     * @MethodAuthor: 聂哥
     * @Date: 2023-03-03 11:49:22
     */
    public static function error($code,$message = null)
    {
        $message = $message ?? StatusCode::getMessage($code);
        return json([
            'code' => $code,
            'msg'  => $message
        ]);
    }
}